<template>
    <div class="light-Ct">
        <!-- 横向Dom -->
        <div class="item-Cd"
            :class="{ 'item-Cd-Dom1': parentChildrenLength && parentChildrenLength > 1 || (parentChildrenLength && parentChildrenLength > 2 && item.children && item.children.length > 2) }">
            <div @click="seeNodeDetail(index, item)" class="light-circle" style="position: relative;" :class="{
                'light-green': item.nodeState == 1,
                'light-red': item.nodeState == 0,
                'light-orange': item.nodeState == 2,
            }">
                <span class="circleTit"> {{ item.percent }} </span>
            </div>
            <div class="item-name">
                <div v-if="!isEditing" @click="startEdit(1)" class="pointers">{{ item.nodeName }}</div>
                <el-input v-else v-model="editedName" @blur="finishEdit" @keyup.enter="finishEdit" ref="editInput1"
                    style="width: 120px;" />
            </div>
            <div class="operation">
                <a @click="addBrotherItem(index, item)" v-if="item.projectId !== 0" title="同级 +"><el-icon>
                        <ArrowRightBold />
                    </el-icon></a>
                <span v-if="item.projectId !== 0">|</span>
                <a @click="addChildItem(index, item)" title="子级 + "><el-icon>
                        <ArrowDownBold />
                    </el-icon></a>
                <span>|</span>
                <a @click="testIntem(index, item)" title="编辑">
                    <el-icon>
                        <EditPen />
                    </el-icon>
                </a>
            </div>
            <!-- <div>父{{ parentChildrenLength }} 子{{ item.children && item.children.length }}</div> -->
            <div class="operation operation2">
                <el-popover placement="bottom" :width="240" trigger="click">
                    <template #reference>
                        <div class="item-btn">
                            <a title="更多"> <el-icon>
                                    <Clock />
                                </el-icon></a>
                        </div>
                    </template>
                    <div class="item-info">
                        <p>开始时间: {{ formatDateTime(item.nodeStartTime) }}</p>
                        <p>结束时间: {{ formatDateTime(item.nodeEndTime) }}</p>
                    </div>
                </el-popover>
                <span>|</span>
                <a @click="deleteItem(index, item)" title="删除"><el-icon>
                        <DeleteFilled />
                    </el-icon></a>
                <span v-if="item.children && item.children.length">|</span>
                <a v-if="item.children && item.children.length" @click="expandCollapse(index, item)" title="收起/展开">
                    <el-icon v-if="item.expanded">
                        <CaretTop />
                    </el-icon>
                    <el-icon v-else>
                        <CaretBottom />
                    </el-icon>
                </a>
            </div>
        </div>

        <!-- 纵向Dom -->
        <div class="item-Cd item-Cd-Dom2">

            <div class="item-Cd-title">
                <div @click="seeNodeDetail(index, item)" class="light-circle" style="position: relative;" :class="{
                    'light-green': item.nodeState == 1,
                    'light-red': item.nodeState == 0,
                    'light-orange': item.nodeState == 2,
                }">
                    <span class="circleTit"> {{ item.percent }} </span>
                </div>
                <div class="item-name">
                    <div v-if="!isEditing" @click="startEdit(2)" class="pointers">{{ item.nodeName }}</div>
                    <el-input v-else v-model="editedName" @blur="finishEdit" @keyup.enter="finishEdit" ref="editInput2"
                        style="width: 200px;" />
                </div>
            </div>
            <!-- <div class="operation">
                <a @click="addBrotherItem(index, item)" v-if="item.projectId !== 0">同级 +</a>
                <span v-if="item.projectId !== 0">|</span>
                <a @click="addChildItem(index, item)">子级 +</a>
                <span>|</span>
                <a @click="deleteItem(index, item)">删除</a>
                <span>|</span>
                <a @click="testIntem(index, item)">编辑</a>
                <span v-if="item.children && item.children.length">|</span>
                <a v-if="item.children && item.children.length" @click="expandCollapse(index, item)">
                    {{ item.expanded ? '收起' : '展开' }}
                </a>
            </div> -->

            <div class="operation">
                <a @click="addBrotherItem(index, item)" v-if="item.projectId !== 0" title="同级 +"><el-icon>
                        <ArrowRightBold />
                    </el-icon></a>
                <span v-if="item.projectId !== 0">|</span>
                <a @click="addChildItem(index, item)" title="子级 + "><el-icon>
                        <ArrowDownBold />
                    </el-icon></a>
                <span>|</span>
                <a @click="testIntem(index, item)" title="编辑">
                    <el-icon>
                        <EditPen />
                    </el-icon>
                </a>
                <span>|</span>
                <a @click="deleteItem(index, item)" title="删除"><el-icon>
                        <DeleteFilled />
                    </el-icon></a>
                <span v-if="item.children && item.children.length">|</span>
                <a v-if="item.children && item.children.length" @click="expandCollapse(index, item)" title="收起/展开">
                    <el-icon v-if="item.expanded">
                        <CaretTop />
                    </el-icon>
                    <el-icon v-else>
                        <CaretBottom />
                    </el-icon>
                </a>
                <span>|</span>
                <el-popover placement="bottom" :width="240" trigger="hover">
                    <template #reference>
                        <div class="item-btn">
                            <a title="更多"><el-icon>
                                    <Clock />
                                </el-icon></a>
                        </div>
                    </template>
                    <div class="item-info">
                        <p>开始时间: {{ formatDateTime(item.nodeStartTime) }}</p>
                        <p>结束时间: {{ formatDateTime(item.nodeEndTime) }}</p>
                    </div>
                </el-popover>
            </div>
        </div>

        <div style="display: flex;align-items: center;justify-content: center;">
            <div v-if="item.expanded && item.children && item.children.length"
                :class="item.children && item.children.length ? 'childItem childItemSon' : 'childItem'"
                >
                <!-- :style="parentChildrenLength && parentChildrenLength == 1 || item.children && item.children.length == 0 ? 'display: block;' : ''" -->
                <RecursiveItem v-for="(child, childIndex) in item.children" :key="childIndex" :item="child"
                    :index="childIndex" @editItem="editItem" @addBrotherItem="addBrotherItem"
                    @addChildItem="addChildItem" @deleteItem="deleteItem" @testIntem="testIntem"
                    @seeNodeDetail="seeNodeDetail" @expandCollapse="expandCollapse"
                    :parentChildrenLength="item.children.length" />
            </div>

        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, defineProps, defineEmits, onUnmounted, nextTick } from 'vue';
import RecursiveItem from './RecursiveItem.vue';
import { formatDateTime } from "@/utils/format";
// import { ElMessage } from 'element-plus';
import { ArrowRightBold, ArrowDownBold, DeleteFilled, EditPen, CaretTop, CaretBottom, Clock } from '@element-plus/icons-vue';

const props = defineProps({
    item: {
        type: Object,
        required: true
    },
    index: {
        type: Number,
        required: true
    },
    parentChildrenLength: { // 新增 prop
        type: Number,
        default: 0
    }
});

const emit = defineEmits(['testIntem', 'editItem', 'addBrotherItem', 'addChildItem', 'deleteItem', 'seeNodeDetail', 'expandCollapse']);

const isEditing = ref(false);
const editedName = ref(props.item.nodeName);
const editInput1 = ref(null);
const editInput2 = ref(null);
const startEdit = (type) => {
    isEditing.value = true;
    nextTick(() => {
        if (isEditing.value) {
            if (type == 1 && editInput1.value) {
                editInput1.value.focus();
            }
            if (type == 2 && editInput2.value) {
                editInput2.value.focus();
            }
        }
    });
};

const finishEdit = () => {
    isEditing.value = false;
    if (!editedName.value) {
        // editedName.value = props.item.nodeName;
        return //ElMessage.error("请输入节点名称");
    }
    if (editedName.value !== props.item.nodeName) {
        emit('editItem', props.index, { ...props.item, nodeName: editedName.value });
    }
};

const testIntem = (index, item) => {
    emit('testIntem', index, item);
};

const editItem = (index, item) => {
    emit('editItem', index, item);
};

const addBrotherItem = (index, item) => {
    emit('addBrotherItem', index, item);
};

const addChildItem = (index, item) => {
    emit('addChildItem', index, item);
};

const deleteItem = (index, item) => {
    emit('deleteItem', index, item);
};

const seeNodeDetail = (index, item) => {
    emit('seeNodeDetail', index, item)
};

const expandCollapse = (index, item) => {
    emit('expandCollapse', index, item);
};

onMounted(() => {

});

onUnmounted(() => {

});

</script>

<style lang="scss" scoped>
.pointers {
    cursor: pointer;
}

// 横向样式
.light-item {
    .light-Ct {
        .item-Cd {
            padding: 10px;
            box-sizing: border-box;
            position: relative;
            min-width: 76px;
            font-size: var(--global-font-size);

            &.item-Cd-Dom2 {
                display: none;
            }

            .light-circle {
                width: 15px;
                height: 15px;
                border-radius: 50%;
                background-color: gray;
                cursor: pointer;
                line-height: 30px;
                margin: auto;
                color: black;
                z-index: 99;

                &.light-green {
                    background-color: green;
                }

                &.light-red {
                    background-color: red;
                }

                &.light-orange {
                    background-color: orange;
                }

                &.light-gray {
                    background-color: gray;
                }

                .circleTit {
                    position: absolute;
                    top: -7px;
                    left: 20px;
                    font-weight: bold;
                }
            }

            .item-btn {
                a {
                    color: var(--el-color-primary);
                    cursor: pointer;
                }

                span {
                    color: #eee;
                    margin: 0px 4px;
                }
            }

            .item-name {
                margin: 10px 0px;
            }

            .operation {
                a {
                    color: var(--global-link-color);
                    cursor: pointer;
                }

                span {
                    color: #eee;
                    // margin: 0px 4px;
                }

                &.operation2 {
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    margin: 10px 0px;
                }
            }

        }
    }

    .childItem {
        position: relative;
        display: flex;

        .item-Cd-Dom1::after {
            content: "";
            position: absolute;
            left: 0px;
            top: 16px;
            width: 100%;
            height: 1px;
            background-color: rgb(43, 38, 38);
        }

        >:first-child>.item-Cd-Dom1::after {
            content: "";
            position: absolute;
            left: unset !important;
            top: 16px;
            width: 50%;
            height: 1px;
            background-color: black;
        }

        >:last-child>.item-Cd-Dom1::after {
            content: "";
            position: absolute;
            left: 0px !important;
            top: 16px;
            width: 50%;
            height: 1px;
            background-color: black;
        }

    }

    .childItemSon>.light-Ct::before {
        content: "";
        position: absolute;
        left: 50%;
        top: -14px;
        width: 1px;
        height: 30px;
        background-color: black;
    }

}

// 纵向样式
.light-item-2 {

    .light-Ct {
        margin: 10px 0px;
        display: flex;
        align-items: center;

        .item-Cd {
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            background-color: #f9f9f9;
            transition: background-color 0.3s;
            // min-width: 236px;

            &.item-Cd-Dom1 {
                display: none;
            }

            &:hover {
                background-color: #eee;
            }

            .item-Cd-title {
                display: flex;
                align-items: center;

                .light-circle {
                    width: 12px;
                    height: 12px;
                    line-height: 30px;
                    text-align: center;
                    border-radius: 50%;
                    margin-right: 10px;
                    color: white;

                    &.light-green {
                        background-color: green;
                    }

                    &.light-red {
                        background-color: red;
                    }

                    &.light-orange {
                        background-color: orange;
                    }

                    .circleTit {
                        position: absolute;
                        top: 14px;
                        left: 0px;
                        font-weight: bold;
                        color: black;
                    }
                }
            }

            .operation {
                margin-left: 22px;
                margin-top: 10px;
                display: flex;

                a {
                    color: var(--global-link-color);
                    text-decoration: none;
                    cursor: pointer;
                }

                span {
                    margin: 0px 4px;
                    color: #ccc;
                }
            }

            .item-name {
                color: var(--global-font-color);
                font-size: var(--global-font-size);
            }
        }

        .childItem {
            margin-left: 24px;
        }

        .childItemSon {
            border-left: 1px dashed #ccc;
            padding-left: 24px;
        }
    }
}

.item-info {
    text-align: left;
    line-height: 20px;
    color: var(--global-font-color);
    font-size: var(--global-font-size);

    p {
        margin: 0px;
    }
}
</style>
